<?php
namespace app\admin\controller;
use app\admin\model\UserGroup as UserGroupModel;
use app\admin\controller\Base;

class UserGroup extends Base
{
    public function index()
    {
        $userGroupRes=db('user_role_rule')->select();
        // var_dump($userGroupRes);
        foreach ($userGroupRes as $k => $v) {
            $data = null;
            foreach (explode(',', $v['rules']) as $val) {
                // var_dump(db('user_rule')->find($val)['title']);
                $data[] = db('user_rule')->find($val)['title'];
            }
            $a[] = $data;
        }
        $this->assign('userGroupRes',$userGroupRes);
        $this->assign('a',$a);
        $this->assign('title','管理员组列表');
        return $this->fetch();
    }


    public function edit()
    {
        if(request()->isPost()){
            $data=input('post.');
            $role = $data['role'];
            /*var_dump($data);
            die;*/
            if($data['rules']){
                $data['rules']=implode(',', $data['rules']);
            }
            /*$_data=array();
            foreach ($data as $k => $v) {
                $_data[]=$k;
            }
            if(!in_array('status', $_data)){
                $data['status']=0;
            }*/
            $save=db('user_role_rule')->where('role'.'='.$role)->update($data);
            if($save!==false){
                $this->success('修改会员组成功！',url('index'));
            }else{
                $this->error('修改会员组失败！');
            }
            return;
        }
        $role = input('role');
        $userGroups=db('user_role_rule')->where('role'.'='.$role)->select();
        $usergroups = $userGroups[0];
        $this->assign('usergroups',$usergroups);
        /*var_dump($userGroups);
        die;*/
        $userRule=new \app\admin\model\UserRule();
        $userRuleRes=$userRule->userRuleTree();

        $this->assign('userRuleRes',$userRuleRes);
        return $this->fetch();
    }

}
